package org.liaohailong.library.util;

import android.graphics.Bitmap;

import androidx.annotation.WorkerThread;

import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;

/**
 * Author: liaohailong
 * Time: 2020/9/22 14:27
 * Describe:
 */
public class BitmapUtil {
    public static byte[] bitmapToRgba(Bitmap bitmap) {
        if (bitmap.getConfig() != Bitmap.Config.ARGB_8888)
            throw new IllegalArgumentException("Bitmap must be in ARGB_8888 format");
        int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];
        byte[] bytes = new byte[pixels.length * 4];
        bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
        int i = 0;
        for (int pixel : pixels) {
            // Get components assuming is ARGB
            int A = (pixel >> 24) & 0xff;
            int R = (pixel >> 16) & 0xff;
            int G = (pixel >> 8) & 0xff;
            int B = pixel & 0xff;
            bytes[i++] = (byte) R;
            bytes[i++] = (byte) G;
            bytes[i++] = (byte) B;
            bytes[i++] = (byte) A;
        }
        return bytes;
    }

    public static Bitmap bitmapFromRgba(int width, int height, byte[] bytes) {
        int[] pixels = new int[bytes.length / 4];
        int j = 0;

        for (int i = 0; i < pixels.length; i++) {
            int R = bytes[j++] & 0xff;
            int G = bytes[j++] & 0xff;
            int B = bytes[j++] & 0xff;
            int A = bytes[j++] & 0xff;

            int pixel = (A << 24) | (R << 16) | (G << 8) | B;
            pixels[i] = pixel;
        }


        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }


    /**
     * 判断图片是否为纯色
     *
     * @param bitmap  检测图片
     * @param percent 严格度 [0.0 ~ 1.0]
     * @return true表示纯色 false表示非纯色
     */
    @WorkerThread
    public static boolean isPureColor(Bitmap bitmap, float percent) {
        if (bitmap.isRecycled()) {
            return false;
        }
        if (bitmap.getConfig() != Bitmap.Config.ARGB_8888) {
            bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, false);
        }

        int bitmapWidth = bitmap.getWidth();
        int bitmapHeight = bitmap.getHeight();
        Buffer buffer = ByteBuffer.allocate(bitmapWidth * bitmapHeight * 4);
        bitmap.copyPixelsToBuffer(buffer);

        String rgbaStringFormat = "%s.%s.%s.%s";
        byte[] pixels = (byte[]) buffer.array();

        Map<String, Integer> colorDic = new HashMap<>();
        int transparencyCount = 0;
        for (int i = 0; i < pixels.length; i += 4) {
            int r = pixels[i] & 0xff; // R
            int g = pixels[i + 1] & 0xff; // G
            int b = pixels[i + 2] & 0xff; // B
            int a = pixels[i + 3] & 0xff; // A

            String rgbaStr = String.format(rgbaStringFormat, r, g, b, a);

            if (a > 0) {
                Integer integer = colorDic.get(rgbaStr);
                if (integer == null || integer == 0) {
                    colorDic.put(rgbaStr, 1);
                } else {
                    int count = integer + 1;
                    colorDic.put(rgbaStr, count);
                }
            } else {
                transparencyCount++;
            }
        }

        int limitCount = (int) (bitmapWidth * bitmapHeight * percent);

        // 最多的一种颜色的个数
        int max = 0;
        for (Map.Entry<String, Integer> entry : colorDic.entrySet()) {
            Integer value = entry.getValue();
            if (value > max) {
                max = value;
            }
        }

        // 当单一颜色的个数+透明像素 > 总像素点的95% 则认为是纯色图片
        return max + transparencyCount > limitCount;
    }
}
